<?php
namespace app\controller;

use support\Request;
use support\Response;

class ShareController
{
    /**
     * 获取分享配置
     * @param Request $request
     * @return Response
     */
    public function getConfig(Request $request): Response
    {
        // 获取页面类型和页面数据
        $pageType = $request->input('page_type', 'index');
        $pageData = $request->input('page_data', []);
        
        // 根据页面类型返回不同的分享配置
        $config = $this->generateShareConfig($pageType, $pageData);
        
        return json([
            'success' => true,
            'data' => $config
        ]);
    }
    
    /**
     * 生成分享配置
     * @param string $pageType 页面类型
     * @param array $pageData 页面数据
     * @return array
     */
    private function generateShareConfig(string $pageType, array $pageData): array
    {
        // 默认配置
        $defaultConfig = [
            'title' => '打牌记账小助手',
            'path' => '/pages/index/index',
            'imageUrl' => '/images/share-room.jpg'
        ];
        
        // 根据页面类型返回不同的配置
        switch ($pageType) {
            case 'index':
                return [
                    'title' => '快来加入我们的游戏房间吧！',
                    'path' => '/pages/index/index',
                    'imageUrl' => '/images/share-room.jpg'
                ];
            
            case 'room':
                $roomCode = $pageData['roomCode'] ?? '';
                return [
                    'title' => "邀请你加入房间 {$roomCode}",
                    'path' => "/pages/room/room?roomCode={$roomCode}",
                    'imageUrl' => '/images/share-room.jpg'
                ];
            
            case 'profile':
                $userInfo = $pageData['userInfo'] ?? [];
                $nickname = $userInfo['nickname'] ?? '玩家';
                return [
                    'title' => "{$nickname}邀请你一起使用打牌记账",
                    'path' => '/pages/profile/profile',
                    'imageUrl' => '/images/share-room.jpg'
                ];
            
            case 'leaderboard':
                return [
                    'title' => '查看我们的排行榜，看看谁是游戏高手！',
                    'path' => '/pages/leaderboard/leaderboard',
                    'imageUrl' => '/images/share-room.jpg'
                ];
            
            case 'settlement':
                $userInfo = $pageData['userInfo'] ?? [];
                $roomInfo = $pageData['roomInfo'] ?? [];
                $nickname = $userInfo['nickname'] ?? '玩家';
                $roomCode = $roomInfo['roomCode'] ?? '';
                return [
                    'title' => "{$nickname}的结算记录 - 房间{$roomCode}",
                    'path' => '/pages/settlement/settlement',
                    'imageUrl' => '/images/share-room.jpg'
                ];
            
            case 'user-records':
                $userInfo = $pageData['userInfo'] ?? [];
                $userStats = $pageData['userStats'] ?? [];
                $nickname = $userInfo['nickname'] ?? '玩家';
                $winRate = $userStats['winRate'] ?? 0;
                return [
                    'title' => "{$nickname}的游戏战绩 - 胜率{$winRate}%",
                    'path' => '/pages/user-records/user-records',
                    'imageUrl' => '/images/share-room.jpg'
                ];
            
            case 'webview':
                $title = $pageData['title'] ?? '查看详情';
                $url = $pageData['url'] ?? '';
                return [
                    'title' => $title,
                    'path' => "/pages/webview/webview?url=" . urlencode($url) . "&title=" . urlencode($title),
                    'imageUrl' => '/images/share-room.jpg'
                ];
            
            default:
                return $defaultConfig;
        }
    }
}